(function($){    
    var aliveTickerPtr = null,
        ALIVE_TICKER_INTERVAL = 10 * 1000;

    /**
     * 客户端会话对象.
     * 1.用户登录后，将user信息放到sessionStorage中，键名为 user。 
     */    
    var _session = {
        /**
         * 客户端打点函数. 
         */
        tick : function(url, data){
            if(data){                
                $.ajax({
                    type : 'POST',
                    url : url,
                    data : data
                });
            } else {
                $.get(url);
            }
        },
        /**
         * 发送测试请求，判断服务器端session是否已失效。 
         */
        ping : function(){
            $.ajax({
                url : '/rest/apis/ping?t={0}'.format( Date.tick() ),
                processUrl : false,
                success : ujs.hr()
            });
        },
        /**
         * 启动client ticker,间隔性地向服务器发送请求,用于保持服务器端Session有效.          
         */
        startAliveTicker : function(){
            var _this = this;
            //keep session effective if uploading operation takes a long time.
            aliveTickerPtr = window.setInterval(function() {
                _this.ping();
            }, ALIVE_TICKER_INTERVAL);
        },
        /**
         * 暂停client ticker. 
         */
        clearAliveTicker : function() {
            if( aliveTickerPtr ) {
                window.clearInterval(aliveTickerPtr);
                aliveTickerPtr = null;
            }
        },        
        /**
         * 获取当前登录的用户信息. 
         */
        getCurrentUserInfo : function(propName){
            var userInfo = sessionStorage.getItem('user');
            if( userInfo ){
                userInfo = JSON.insure(userInfo);
                return Type.isUndefined(propName) ? userInfo : userInfo[propName];                    
            }
        },
        /**
         * 设置当前登录的用户信息.  
         */
        setCurrentUserInfo : function(newUserInfo, isOldUserInfoDefault){            
            var newUserInfoTmp = {};            
            if(isOldUserInfoDefault !== false){
                var userInfo = sessionStorage.getItem('user');
                if( userInfo ){
                    newUserInfoTmp = JSON.insure( userInfo );
                }
            }            
            for(var key in newUserInfo){                
                newUserInfoTmp[key] = newUserInfo[key];                
            }
            //设置默认用户头像.
            if(!newUserInfoTmp['icon']){
                newUserInfoTmp['icon'] = ujs.getConfigRes('defUserIcon');
            }
            sessionStorage.setItem("user", JSON.stringify(newUserInfoTmp));
            this.fire('userInfoChanged', newUserInfo);
        },
        /**
         * 用户ID. 
         */
        getCurrentUserID : function(){
            var userInfo = this.getCurrentUserInfo();
            if( userInfo ){
                return userInfo['id'] || userInfo['ID'] || userInfo['uid'] || userInfo['UID'];
            }
        },
        getCurrentUserIcon : function(){
            return this.getCurrentUserInfo()['icon'] || '';  
        },
        /**
         * 判断指定的uid是否是当前用户. 
         * @param {String} uid
         */
        isCurrentUser : function(uid){
            if(Type.isObject(uid)){
                var userObj = uid;
                uid = userObj['id'] || userObj['uid'];
            }
            return uid ? this.getCurrentUserID() === uid : false;
        }        
    };
    Observable.enable(_session);
    
    //------------------------------------------------------------------------------------
    
    ujs.define('session', 'ujs.util', function(){
        return _session;
    });
})($);